const Auth = require('../../utils/auth');
const Api = require('../../utils/api.js');
const Rest = require('../../utils/rest');
const util = require('../../utils/util.js');
Page({

    data: {
        setting: {},
        user: undefined,
        menu: {},
        bh: '', // 当前小时
        Greetings:"",//问候语
    },

    default: {
        background: Api.JIANGQIE_BG_MY,
        description2: '菜单项请在插件后台-我的设置中配置！',
        description3: '版本号请在插件后台-我的设置中配置！', 
        menu: [ //views,likes,favorites,comments,about,feedback,contact,clear,split,link,page
            {
                tp: 'views',
                icon: '../../images/icon_view.png',
                title: '我的浏览',
                line: 1,
            },
            {
                tp: 'likes',
                icon: '../../images/icon_like.png',
                title: '我的点赞',
                line: 1,
            },
            {
                tp: 'favorites',
                icon: '../../images/icon_fav.png',
                title: '我的收藏',
                line: 1,
            },
            {
                tp: 'comments',
                icon: '../../images/icon_com.png',
                title: '我的评论',
                line: 0,
            },
            {
                tp: 'about',
                icon: '../../images/icon/about.png',
                title: '关于我们',
                line: 1,
            },
            {
                tp: 'yqlj',
                icon: '../../images/icon/yqlj.png',
                title: '友情链接',
                line: 1,
            },
            {
                tp: 'feedback',
                icon: '../../images/icon/fk.png',
                title: '反馈帮助',
                line: 1,
            },
            {
                tp: 'gxrz',
                icon: '../../images/icon/gx.png',
                title: '更新日志',
                line: 1,
            },
            {
                tp: 'mzsm',
                icon: '../../images/icon/mzsm.png',
                title: '免责声明',
                line: 1,
            },
            {
                tp: 'openSetting',
                icon: '../../images/icon/qxsz.png',
                title: '权限设置',
                line: 1,
            },
            {
                tp: 'clear',
                icon: '../../images/icon/hc.png',
                title: '清理缓存',
                line: 0,
            },
        ]
    },
    onLoad(){
        let that = this;
            util.getshare(that);
    },
    onShow: function (options) {
        let that = this;
        let user = Auth.getUser();
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;

        //获取当前时间
        var n = timestamp * 1000;
    
        var date = new Date(n);
        //获取时
        var h = date.getHours();
        
        console.log("现在的时间是"+h+"点")
    
      
        if (0 < h && h <= 6) {
          console.log("  0：00—6:00凌晨:勤奋的你")
          that.setData({
            bh: '凌晨了，',
            Greetings:"凌晨勤奋的你，要注意身体哟！"
          })
        } else if (6 <= h && h < 9) {
          console.log("6：00—6:00早上:奋斗的你")
          that.setData({
            bh: '早上好，',
            Greetings:"早起奋斗的你，加油哦！"
          })
        }
        else if (9 <= h && h < 11) {
            console.log("6：00—6:00早上:奋斗的你")
            that.setData({
              bh: '早上好，',
              Greetings:"时间一分一秒的溜走，再也不回来！"
            })
        }
        else if (11 <= h && h <= 14) {
         
          console.log("11：00—13:00中午:激情的你")
          that.setData({
            bh: '中午好，',
            Greetings:"再忙再累，也要按时吃饭哦！"
          })
        } else if (14 <= h && h <= 16) {
        
          console.log("18:00—24:00下午:懒洋洋的你")
          that.setData({
            bh:'下午好，',
            Greetings:"有点懒洋洋哦！打起精神来！"
          })
        }
        else if (16 <= h && h <= 18) {
          console.log("16：00：00—18:00傍晚:活力的你")
          that.setData({
            bh: '晚上好，',
            Greetings:"坚持一会，下班后就会充满活力！"
          })
        }
        else if (18 <= h && h <= 21) {
            console.log("16：00：00—18:00傍晚:活力的你")
            that.setData({
              bh: '晚上好，',
              Greetings:"在看一会就要去休息哦！身体重要！"
            })
          }
        else {
           console.log("晚上啦，记得好好照顾自已，别熬夜")
            that.setData({
                bh: '晚上好，',
                Greetings:"晚上啦，记得好好照顾自已，别熬夜！"
          })
        }
        that.setData({
            user: user
        });

        Rest.get(Api.JIANGQIE_SETTING_UCENTER).then(res => {
            let menu = that.default.menu;
            if (res.data.menu.length > 0) {
                menu = res.data.menu;
            }
            that.setData({
                setting: {
                    background: res.data.background ? res.data.background : that.default.background,
                    description: res.data.description ? res.data.description : that.default.description,
                    description2: res.data.description2 ? res.data.description2 : that.default.description2,
                    description3: res.data.description3 ? res.data.description3 : that.default.description3,
                    description4: res.data.description4 ? res.data.description4 : that.default.description4,
                    title: res.data.title ? res.data.title : that.default.title,
                },
                menu: menu
            });
        })
    },

    onShareAppMessage: function () {
        var that = this;
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
        return {
            title: that.data.shares.share_title,
            imageUrl: that.data.shares.share_image,
        }
    },
    //转发朋友圈
    onShareTimeline: function () {
        var that = this;
        return {
            title: that.data.shares.share_title,
            imageUrl: that.data.shares.share_image,
        }
    },
    // 收藏
    onAddToFavorites:function(){
        var that = this;
        return {
            title: that.data.shares.share_title,
            imageUrl: that.data.shares.share_image,
        }
    },

    handlerPostTrack: function (e) {
        if (!Auth.getUser()) {
            this.setData({
                showPopLogin: true
            });
            return;
        }

        let track = e.currentTarget.dataset.track;
        wx.navigateTo({
            url: '/pages/list/list?track=' + track
        })
    },

    handlerAbout: function (e) {
        wx.navigateTo({
            url: '/pages/about/about',
        })
    },
    handleryqlj: function (e) {
        wx.navigateTo({
            url: '/pages/yqlj/yqlj',
        })
    },
    handlerfeedback: function (e) {
        wx.navigateTo({
            url: '/pages/feedback/feedback',
        })
    },
    login: function (e) {
        wx.navigateTo({
            url: '../login/login',
        })
    },
    /*log*/ 
    log: function (e) {
        wx.navigateTo({
            url: '/pages/log/log',
        })
    },
    /*log*/
    handlerClearCache: function (e) {
        wx.showModal({
            title: '缓存清理',
            content: '清除缓存,需要重新授权登录！',
            success(res) {
                if (res.confirm) {
                    wx.clearStorageSync();
                    wx.showToast({
                        title: '缓存清除成功！',
                        image: '../../images/hc.png',
                    });
                    wx.reLaunch({
                        url: '/pages/index/index',
                    })
                }
            }
        });
    },
    //*权限设置
    openSetting: function () {
        let that=this;
        wx.openSetting({
          success: function (osrs) {
            // 出发条件是返回的时候
    console.log("Aaaa");
            wx.getUserInfo({
              success: function (getuserinfo) {
                that.data.hasuserinfo = true;
                that.setData(that.data);
              },
              fail: function (fres) {
                that.data.hasuserinfo = false;
                that.setData(that.data);
              }
            })
          }
        })
      },
    handlerLinkClick: function (e) {
        let link = e.currentTarget.dataset.link;
        if (link.startsWith('/pages')) {
            wx.navigateTo({
                url: link,
            })
        } else {
            wx.navigateToMiniProgram({
                appId: link,
                fail: res => {
                    wx.showToast({
                        title: '无效链接',
                    })
                }
            })
        }
    },
    handlerPageClick: function (e) {
        let page_id = e.currentTarget.dataset.page_id;
        wx.navigateTo({
            url: '/pages/viewhtml/viewhtml?page_id=' + page_id,
        })
    },
    handlerLoginCancelClick: function (e) {
        this.setData({
            showPopLogin: false
        });
    },
    handlerDoLoginClick: function (e) {
        wx.navigateTo({
            url: '/pages/login/login',
        });

        this.setData({
            showPopLogin: false
        });
    },
})
